package io.jee.caisan.weixin.handler;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.builder.outxml.NewsBuilder;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class SubscribeHandler extends AbstractHandler {

	@Resource
	private WxMpService wxMpService;
	
	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) throws WxErrorException {
		//WxMpMaterialNews news = wxMpService.getMaterialService().materialNewsInfo("sHlewoQ_e7_aSqrJktREevhe7X27J1yzbXdx3EZb3gc");
		//List<WxMpMaterialNewsArticle> articles = news.getArticles();
		NewsBuilder newsBuilder = new NewsBuilder();
		//for (WxMpMaterialNewsArticle wxMpMaterialNewsArticle : articles) {
			WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
			item.setTitle("欢迎关注");
			item.setPicUrl("http://mmbiz.qpic.cn/mmbiz_jpg/zhW3YYfZp451vxSu5tPQbEwjZFg78T1BmL3OpYIEmTibTXmzMfg9HNQ2Vr4Tgp0qjiceM3jbRUBAV66qoQwNcOCA/0?wx_fmt=jpeg");
			item.setUrl("http://mp.weixin.qq.com/s?__biz=MzUxMzI4NzI1OQ==&mid=100000001&idx=1&sn=51f1d9a3a72c0b89d53f78c1bec8e4d6&chksm=795634354e21bd2363e08965b22687446915924f03c89c9e9134141d89e5fc40c09c38ea58d7#rd");
			item.setDescription("彩伞公众号，方便快捷管理您的彩伞。");
			newsBuilder.addArticle(item);
		//}
		return newsBuilder.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
	}

	/**
	 * 处理特殊请求，比如如果是扫码进来的，可以做相应处理
	 */
//	private WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage) throws Exception {
//		// TODO
//		return null;
//	}

}
